//
//  ReadViewController.m
//  Leisure
//
//  Created by lanou on 16/6/28.
//  Copyright © 2016年 张凡. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArray;//数据源
@property (nonatomic, strong)NSMutableArray *scrollArray;//轮播图图片
@end

@implementation ReadViewController

- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)scrollArray{
    
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        //设置item大小
        layout.itemSize = CGSizeMake(100, 100);
        //设置间距
        layout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3 + 64, kWidth, kHeight/2) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [RequsetManager fetchDataWithUrl:kReadUrl parametr:@{} mehtod:@"GET" complete:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        //添加轮播图的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {
            
            [self.scrollArray addObject:smallDic[@"img"]];
        }
        
        //添加collectionView数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            
            ReadModel *readModel = [[ReadModel alloc] init];
            [readModel setValuesForKeysWithDictionary:smallDic];
            [self.dataArray addObject:readModel];
        }
        
    } updateUI:^{
        
        //创建轮播图
        ZFScrollView *scrollView = [[ZFScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3) imageArray:self.scrollArray];
        [self.view addSubview:scrollView];
        
        [self.collectionView reloadData];
    }];
    
    //注册XIB
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"read_cell"];
    
    [self.view addSubview:self.collectionView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
}

#pragma mark ******CollectionViewDelegate*******************
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
    //设置cell
    [cell setCellWithModel:self.dataArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //通过XIB来获取控制器
    ReadListViewController *readListVC = [[ReadListViewController alloc] initWithNibName:@"ReadListViewController" bundle:nil];
    
    ReadModel *model = self.dataArray[indexPath.row];
    readListVC.typeID = model.type;
    
    [self.navigationController pushViewController:readListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
